package com.tfleo.mealssenger;

import java.util.ArrayList;
import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.adapter.UserListAdapter;
import com.tfleo.mealssenger.adapter.WantedListAdapter;
import com.tfleo.mealssenger.object.Food;
import com.tfleo.mealssenger.object.User;
import com.tfleo.mealssenger.utility.DataFetcher;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class WantedBrowseActivity extends Activity {
	private LayoutInflater mInflater;
	private SharedPreferences mSharedPref;
	
	private ListView vWantedList;
	
	private int mUserId;
	private boolean mIsMine = false;
	
	private ArrayList<Food> mWantedList;
	private WantedListAdapter mWantedListAdapter;
	
	private Context mContext;
	private String mAuth;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wishlist_layout);
		
		mContext = this;
		mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		mSharedPref = getSharedPreferences(Main.USER_DATA, MODE_PRIVATE);
		mAuth = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID); //for authentication
		
		vWantedList = (ListView) findViewById(R.id.wanted_list);
		
		Bundle extras = getIntent().getExtras();
		if(extras != null) {
			mUserId = extras.getInt("id", 0);
			if(mUserId == mSharedPref.getInt(Main.USER_ID, 0)) {
				mIsMine = true;
			}
		} else {
			Toast.makeText(mContext, "Error: Bundle is null!", Toast.LENGTH_SHORT).show();
			finish();
		}
		
		FetchWantedTask friendTask = new FetchWantedTask();
		friendTask.execute(mUserId); 
	}
	
	private class FetchWantedTask extends AsyncTask<Integer, Void, Boolean> {
		private ProgressDialog mProgressDialog;
		@Override    
	    protected void onPreExecute() 
	    {       
	        super.onPreExecute();
	        mProgressDialog = new ProgressDialog(mContext);
	        mProgressDialog.setMessage("Loading...");
	        mProgressDialog.setCancelable(false);
	        mProgressDialog.setIndeterminate(true);
	        mProgressDialog.show();
	    }
		
		@Override
		protected Boolean doInBackground(Integer... id) {
			mWantedList = DataFetcher.fetchWantedFoods(id[0], mSharedPref.getInt(Main.USER_ID, 0), mAuth);
			if(mWantedList == null) {
				return false;
			}
			return true;
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			if(result) {
				mWantedListAdapter = new WantedListAdapter(mContext,
						R.layout.wishlist_entry, R.id.name, mWantedList, mInflater, mIsMine, mUserId, mAuth);
				vWantedList.setAdapter(mWantedListAdapter);
				vWantedList.setOnItemClickListener(new OnItemClickListener() {
                	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {         
                		Intent i = new Intent(WantedBrowseActivity.this, FoodActivity.class); 
                		i.putExtra("id", mWantedList.get(position).id);
                		i.putExtra("name", mWantedList.get(position).name); 
                		i.putExtra("location_id", mWantedList.get(position).locationId);
                		i.putExtra("location_name", mWantedList.get(position).locationName);
                		i.putExtra("description", mWantedList.get(position).description);
                		i.putExtra("category", mWantedList.get(position).category);
						i.putExtra("image", mWantedList.get(position).imageUrl);
						i.putExtra("likes", mWantedList.get(position).numEaten);
                		startActivity(i);
                	}
                });
				mProgressDialog.dismiss();
    		} else {
    			Toast.makeText(mContext, "Error: Connection with server failed!", Toast.LENGTH_SHORT).show();
    			mProgressDialog.dismiss();
    		}
	    }
		
	}

	
}
